package com.bobo.myapplication.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.bobo.myapplication.bean.WheelBean;

import java.util.ArrayList;
import java.util.List;
/**
 * Created by ZJB on 2018/1/5.
 *
 * @ copyright: iwhere chengdu technology
 */
public class Wheelview extends View {
    private int wheelViewHeight;
    private int wheelViewWidth;
    private int showViewCount = 7;
    private int maxTextSize = 40;
    private float textScale = 1.0f;
    private Context mContext;
    private Paint mPaint;
    private int firstItemLineHight;
    private int lastItemLineHeight;
    private int contentHight;
    private List<WheelBean> datas = new ArrayList<>();
    private String TAG = Wheelview.class.getSimpleName();
    private int initFirstHeight;
    private float initLessTextScale;

    public Wheelview(Context context) {
        this(context, null);
    }

    public Wheelview(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Wheelview(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        setTestData();
    }

    private void setTestData(){
        datas = new ArrayList<>();
        datas.add(new WheelBean("成都银行",null));
        datas.add(new WheelBean("西安银行",null));
        datas.add(new WheelBean("北京银行",null));
        datas.add(new WheelBean("自贡银行",null));
        datas.add(new WheelBean("绵阳银行",null));
        datas.add(new WheelBean("乐山银行",null));
        datas.add(new WheelBean("重庆银行",null));
        datas.add(new WheelBean("杭州银行",null));
        datas.add(new WheelBean("南京银行",null));
        datas.add(new WheelBean("上海银行",null));
        datas.add(new WheelBean("湖北银行",null));
        datas.add(new WheelBean("武汉银行",null));
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.GRAY);
        mPaint.setTextSize(maxTextSize);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        if (modeWidth != MeasureSpec.EXACTLY) {
            width = (int) (maxTextSize  + getPaddingLeft() + getPaddingRight());
        }
        wheelViewWidth = width;

        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (modeHeight != MeasureSpec.EXACTLY) {
            height = (int) (maxTextSize * showViewCount + getPaddingBottom() + getTop());
        }
        wheelViewHeight = height;
        setMeasuredDimension(width, height);

        calculate();
    }

    private void calculate(){
        contentHight = (int) (maxTextSize);
        initFirstHeight = (int) (getPaddingTop() + ((showViewCount/2 + 1) * contentHight));
        firstItemLineHight = initFirstHeight;
        lastItemLineHeight = (int) (getPaddingBottom() + contentHight);

        initLessTextScale = 1.0f - (showViewCount/2 -1) * 0.1f;

    }

    private float downY = 0f;
    private float stepMoveY = 0f;
    private float stepDownY = 0f;
    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e(TAG, "onTouchEvent: event:"+event.getAction() );
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                downY = event.getY();
                stepDownY = event.getY();
                stepMoveY = 0f;
                break;
            case MotionEvent.ACTION_MOVE:
                float moveY = event.getY() - downY;
                downY = event.getY();

                stepMoveY = event.getY() - stepDownY;
                Log.e(TAG, "onTouchEvent: moveY:"+ moveY +" stepMoveY:"+stepMoveY);
                handleMove(moveY);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                handleUp();
                break;
        }
        return true;
    }

    private int getStartX(int length,int textSize){
        int x = wheelViewWidth - length * textSize;
        return x > 0?x/2:0;
    }

    private void handleUp() {
        int i = firstItemLineHight % contentHight;
        int i1 = (int) Math.round(firstItemLineHight * 1.0 / contentHight);
        Log.e(TAG, "handleUp: i1："+firstItemLineHight * 1.0/ contentHight +" :"+i1);
        if(i != 0){
            firstItemLineHight = contentHight * i1;
        }
        invalidate();
        Log.e(TAG, "handleUp: 当前选中了："+datas.get(getPosition()).getTitle());
    }

    private void handleMove(float moveY) {
        firstItemLineHight += moveY;
        if(firstItemLineHight > initFirstHeight){
            firstItemLineHight = initFirstHeight;
        }else if(firstItemLineHight < initFirstHeight - (datas.size()-1)*contentHight){
            firstItemLineHight = (int) (initFirstHeight - (datas.size()-1)*contentHight);
        }

//        textScale = stepMoveY

        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        canvas.drawPath();
//        Path path = new Path();
//        path.addArc();
//        path.lineTo();


        if (datas == null || datas.size() <= 0) {
            Log.e(TAG, "没有数据可以绘制");
            return;
        }
        Log.e(TAG, "onDraw: firstItemLineHight:" + firstItemLineHight + " contentHight:" + contentHight + " getPosition:" + getPosition());
        for (int i = 0; i < datas.size(); i++) {
            String title = "";
            if (i < datas.size()) {
                title = datas.get(i).getTitle(); ////aadsfa
            }
            setTextLineColor(i, canvas);
            setSelecedTextColor(i);
            setTextSize(i);


            int startX = getStartX(title.length(), (int) mPaint.getTextSize());
            if (i == 0) {
                canvas.drawText(title, startX, firstItemLineHight-5, mPaint);
            } else {
                canvas.drawText(title, startX, (firstItemLineHight + i * contentHight)-5, mPaint);
            }
        }
    }

    private void setTextSize(int i) {
        if(isSelected(i)){
            textScale = 1.0f;
        }else if (isLessSelectedIndex(i)){
            int i1 = getSelectedPositon() - i;
            Log.e(TAG, "setTextSize: getSelectedPositon:"+getSelectedPositon()+ " i1:"+i1 );
            textScale = (float) (1.0f - i1 * 0.15);
        }else {
            textScale -= 0.15f;
            if(textScale < 0.5f){
                textScale = 0.5f;
            }
        }
        mPaint.setTextSize(maxTextSize*textScale);
    }

    private void setSelecedTextColor(int i) {
        if(isSelected(i)){
            mPaint.setColor(Color.GREEN);
        }else {
            mPaint.setColor(Color.GRAY);
        }
    }

    private boolean isSelected(int i) {
        float v = ((initFirstHeight - firstItemLineHight) * 1.0f / contentHight);
        return (Math.round(v)) % datas.size() == i;
    }

    private boolean isLessSelectedIndex(int i){
        float v = ((initFirstHeight - firstItemLineHight) * 1.0f / contentHight);
        return (Math.round(v)) % datas.size() > i;
    }
    private int getSelectedPositon(){
        float v = ((initFirstHeight - firstItemLineHight) * 1.0f / contentHight);
        return (Math.round(v)) % datas.size();
    }

    private void setTextLineColor(int i, Canvas canvas) {
        if(i == showViewCount/2){
            mPaint.setColor(Color.GREEN);
            canvas.drawLine(0,i*contentHight,wheelViewWidth,i*contentHight,mPaint);
            canvas.drawLine(0,(i+1)*contentHight+5,wheelViewWidth,(i+1)*contentHight+5,mPaint);
            mPaint.setColor(Color.GRAY);
        }
    }

    public List<WheelBean> getDatas() {
        return datas;
    }

    public void setDatas(List<WheelBean> datas) {
        this.datas = datas;
        invalidate();
    }

    public void moveTo(int position){
        firstItemLineHight = (int) (getPaddingTop() + ((position + 1) * contentHight));
        invalidate();
    }

    public int getPosition(){
        return (initFirstHeight - firstItemLineHight)/50;
    }

}
